﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.BlackApart.Data.Caching {
	public class CachedApartmentGroupDataAccess : IApartmentGroupDataAccess {

		#region Fields

		private IApartmentGroupDataAccess decoratedDataAccess;
		private NemoCache cache;

		#endregion

		#region Constructors

		public CachedApartmentGroupDataAccess(IApartmentGroupDataAccess decoratedDataAccess, string connectionStringToDecoretedDb) {
			this.decoratedDataAccess = decoratedDataAccess;
			this.cache = new NemoCache(TimeSpan.FromMinutes(2), connectionStringToDecoretedDb);
		}

		#endregion

		#region Methods

		public SeoData GetApartmentGroupSeo(int apartmentGroupId, string language) {
			cache.Reset();
			cache.SetDependencyTables("T28");
			cache.SetParameters(apartmentGroupId);
			cache.SetKey("GetApartmentGroupSeo" + language);
			SeoData data = cache.GetData<SeoData>();
			if (string.IsNullOrEmpty(data.PageTitle)) {
				data = this.decoratedDataAccess.GetApartmentGroupSeo(apartmentGroupId, language);
				cache.SetCaching(data);
			}
			return data;
		}

		public int SaveApartmentGroup(int id, string name, int numberOfPeople, int locationId, float areaSize) {
			this.cache.TouchTimes();
			return this.decoratedDataAccess.SaveApartmentGroup(id, name, numberOfPeople, locationId, areaSize);
		}

		public ApartmentGroupData GetApartmentGroup(int groupId, string language) {
			cache.Reset();
			cache.SetDependencyTables("T28");
			cache.SetParameters(groupId);
			cache.SetKey("GetApartmentGroup" + language);
			ApartmentGroupData data = cache.GetData<ApartmentGroupData>();
			if (data.Id == 0) {
				data = this.decoratedDataAccess.GetApartmentGroup(groupId, language);
				cache.SetCaching(data);
			}
			return data;
		}

		public ApartmentGroupData[] GetApartmentGroups(string language) {
			cache.Reset();
			cache.SetDependencyTables("T28");
			cache.SetKey("GetApartmentGroups");
			cache.SetParameters(language);
			ApartmentGroupData[] data = cache.GetData<ApartmentGroupData[]>();
			if (data == null) {
				data = this.decoratedDataAccess.GetApartmentGroups(language);
				cache.SetCaching(data);
			}
			return data;
		}

		public int DeleteApartmentGroup(int groupId) {
			this.cache.TouchTimes();
			return this.decoratedDataAccess.DeleteApartmentGroup(groupId);
		}

		public bool CheckExists(int groupId) {
			this.cache.TouchTimes();
			return this.decoratedDataAccess.CheckExists(groupId);
		}

		public Dictionary<string, object> GetApartmentGroupProperties(int apartmentGroup, string language) {
			cache.Reset();
			cache.SetDependencyTables("T28");
			cache.SetKey("GetApartmentGroupProperties" + language);
			cache.SetParameters(apartmentGroup);
			Dictionary<string, object> data = cache.GetData<Dictionary<string, object>>();
			if (data == null) {
				data = this.decoratedDataAccess.GetApartmentGroupProperties(apartmentGroup, language);
				cache.SetCaching(data);
			}
			return data;
		}

		public Dictionary<string, string> GetApartmentGroupArrayProperties(int apartmentGroup, string propertyKey, string language) {
			cache.Reset();
			cache.SetDependencyTables("T28");
			cache.SetKey("GetApartmentGroupArrayProperties");
			cache.SetParameters(apartmentGroup, propertyKey, language);
			Dictionary<string, string> data = cache.GetData<Dictionary<string, string>>();
			if (data == null) {
				data = this.decoratedDataAccess.GetApartmentGroupArrayProperties(apartmentGroup, propertyKey, language);
				cache.SetCaching(data);
			}
			return data;
		}


		public ApartmentOptionFieldData[] GetApartmentGroupOptionFields(int groupId, string language) {
			cache.Reset();
			cache.SetDependencyTables("T28", "T256");
			cache.SetParameters(groupId, language);
			cache.SetKey("GetApartmentGroupOptionFields");
			ApartmentOptionFieldData[] data = cache.GetData<ApartmentOptionFieldData[]>();
			if (data == null) {
				data = this.decoratedDataAccess.GetApartmentGroupOptionFields(groupId, language);
				cache.SetCaching(data);
			}
			return data;
		}

		#endregion

	}
}
